package mes.kanban.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import mes.kanban.entity.MesPermission;
import mes.kanban.entity.MesRole;
import mes.kanban.entity.MesRoleAndJob;
import mes.kanban.entity.MesRoleAndPermission;
import mes.kanban.entity.MesUserAndRole;
import mes.kanban.respository.RoleAndJobRespository;
import mes.kanban.respository.RoleAndPermissionRespository;
import mes.kanban.respository.UserAndRoleRespository;
import mes.kanban.respository.UserPermissionRespository;
import mes.kanban.respository.UserRoleRespository;
import mes.kanban.service.MesRoleServiceI;
import mes.kanban.util.IsNotNullValit;
import mes.kanban.util.PageBean;
import mes.kanban.vo.MesRoleAndPermissionVO;
import mes.kanban.vo.MesRoleVO;
import mes.kanban.vo.MesUserAndRoleVO;

/**
 * 角色管理
 * @author zongrui.liu
 *
 */
@Service
public class MesRoleServiceImpl implements MesRoleServiceI {
	
	@Autowired
	private UserRoleRespository userRoleRespository;
	
	@Autowired
	private UserAndRoleRespository userAndRoleRespository;
	
	@Autowired
	private UserPermissionRespository userPermissionRespository;
	
	@Autowired
	private RoleAndPermissionRespository roleAndPermissionRespository;
	
	@Autowired
	private RoleAndJobRespository roleAndJobRespository;

	@Override
	public PageBean getRolesForBean(Integer pageNum, Integer pageSize) {
		
		PageBean pb = new PageBean();
		Pageable pageable = new PageRequest(pageNum-1, pageSize);
		
		Page<MesRole> ps = this.userRoleRespository.findAll(pageable);
		
		List<MesRole> us = ps.getContent();
		
		int allRow = (int) ps.getTotalElements();
		int totalPage = ps.getTotalPages();
		
		pb.setAllCounts(allRow);
		pb.setPageSize(pageSize);
		pb.setCurrentPage(pageNum);
		pb.setTotalPage(totalPage);
		pb.setList(us);
		
		return pb;
	}

	@Override
	public void save(MesRoleVO vo) {
		MesRole role  = new MesRole();
		BeanUtils.copyProperties(vo, role);
		this.userRoleRespository.save(role);
	}

	@Override
	public void edit(MesRoleVO vo) {
		MesRole role  = new MesRole();
		BeanUtils.copyProperties(vo, role);
		this.userRoleRespository.save(role);
	}
	
	@Override
	public void deleteRoleById(String id) {
		List<MesRoleAndPermission> perMissionList = roleAndPermissionRespository.findByRoleId(id);
		List<MesRoleAndJob> jobList = roleAndJobRespository.findByRoleId(id);
		if(IsNotNullValit.CollectionIsNotNull(perMissionList)) {
			roleAndPermissionRespository.delete(perMissionList);
		}
		if(IsNotNullValit.CollectionIsNotNull(jobList)) {
			roleAndJobRespository.delete(jobList);
		}
		userRoleRespository.delete(id);
	}
	
	@Override
	public List<MesUserAndRoleVO> getRoleList() {
		List<MesUserAndRoleVO> vos = new ArrayList<MesUserAndRoleVO>();
		List<MesRole> list = userRoleRespository.findAll();
		for(MesRole role :list) {
			MesUserAndRoleVO vo = new MesUserAndRoleVO();
			BeanUtils.copyProperties(role, vo);
			vos.add(vo);
		}
		return vos;
	}

	@Override
	public List<MesUserAndRoleVO> getRoleListByUserId(String id) {
		List<MesUserAndRoleVO> vos = new ArrayList<MesUserAndRoleVO>();
		List<MesRole> list = userRoleRespository.getRoleListByUserId(id);
		for(MesRole role :list) {
			MesUserAndRoleVO vo = new MesUserAndRoleVO();
			BeanUtils.copyProperties(role, vo);
			vos.add(vo);
		}
		return vos;
	}
	
	@Override
	public void saveUserAndRole(List<MesUserAndRole> roleList) {
		
		if (IsNotNullValit.CollectionIsNotNull(roleList)) {
			
			//先删除用户与角色的对应关系
			this.userRoleRespository.deleteRoleAndUserByUserId(roleList.get(0).getUserId());
			
			for (MesUserAndRole mesUserAndRole : roleList) {
				this.userAndRoleRespository.save(mesUserAndRole);
			}
		}
		
	}
	
	@Override
	public void deleteRoleByuserId(String userId) {
		//先删除用户与角色的对应关系
		this.userRoleRespository.deleteRoleAndUserByUserId(userId);
	}
	
	@Override
	public List<MesRoleAndPermissionVO> getPermissionList() {
		
		List<MesRoleAndPermissionVO> vos = new ArrayList<MesRoleAndPermissionVO>();
		List<MesPermission> list = userPermissionRespository.findAll();
		for(MesPermission permission :list) {
			MesRoleAndPermissionVO vo = new MesRoleAndPermissionVO();
			BeanUtils.copyProperties(permission, vo);
			vos.add(vo);
		}
		return vos;
		
	}
	
	@Override
	public List<MesRoleAndPermissionVO> getPermissionByRoleId(String id) {
		List<MesRoleAndPermissionVO> vos = new ArrayList<MesRoleAndPermissionVO>();
		List<MesPermission> list = userPermissionRespository.getPermissionByRoleId(id);
		for(MesPermission permission :list) {
			MesRoleAndPermissionVO vo = new MesRoleAndPermissionVO();
			BeanUtils.copyProperties(permission, vo);
			vos.add(vo);
		}
		return vos;
	}

	@Override
	public void saveRoleAndPermission(List<MesRoleAndPermission> permissionList) {
		
		//先删除角色和权限的对应关系
		if (IsNotNullValit.CollectionIsNotNull(permissionList)) {
			this.userRoleRespository.deleteRoleAndPermissionByRoleId(permissionList.get(0).getRoleId());
			for (MesRoleAndPermission mesRoleAndPermission : permissionList) {
				this.roleAndPermissionRespository.save(mesRoleAndPermission);
			}
		}
		
	}
	@Override
	public void deleteRoleAndPermissionByRoleId(String id) {
		this.userRoleRespository.deleteRoleAndPermissionByRoleId(id);
	}

	@Override
	public void saveRoleAndJob(List<MesRoleAndJob> jobList) {
		//先删除角色和权限的对应关系
		if (IsNotNullValit.CollectionIsNotNull(jobList)) {
			this.userRoleRespository.deleteRoleAndJobByRoleId(jobList.get(0).getRoleId());
			for (MesRoleAndJob mesRoleAndJob : jobList) {
				this.roleAndJobRespository.save(mesRoleAndJob);
			}
		}
	}

	@Override
	public void deleteRoleAndJobByRoleId(String id) {
		this.userRoleRespository.deleteRoleAndJobByRoleId(id);
	}


}
